package org.example.course.observer;

import org.example.commonapi.domain.Inbox;
import org.example.commonapi.dto.CourseStudentDto;
import org.example.commonapi.util.options.InboxService;
import org.example.course.service.CourseClassService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

// InboxServiceObserver.java
@Component
public class InboxServiceObserver implements InboxObserver {
    @Resource
    private InboxService inboxService;
    @Resource
    private CourseClassService courseClassService;

    @Override
    public void update(Long courseId, String name, Long objectId, Integer type,
                       LocalDateTime createTime, LocalDateTime endTime) {

        List<CourseStudentDto> studentDtoList = courseClassService.courseClassStudentList(courseId);
        List<Inbox> inboxList = studentDtoList.stream()
                .map(dto -> new Inbox(name, dto.getId(), objectId, type, createTime, endTime))
                .collect(Collectors.toList());
        inboxService.saveBatch(inboxList);
    }
}